/* ============================================================================
* 功能描述 ：主函数
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/

#include "main.h"
unsigned char RxTable[10],rxDADA;
int time_f;
int now_time_f;

int last_time_f;

bit key_flag;

bit RXOK_f;

static void _data_tx_pack(void)
{
#if 0
	char app_data[10];
	unsigned int ss;
	ss = Read_ADC(14, 0);
	app_data[0] = SHORT_GET_HIGH_BYTE(ss);
	app_data[1] = SHORT_GET_LOW_BYTE(ss);
	app_data[2] = ss;
	app_data[3] = 0x04;
	app_data[4] = 0x05;
	app_data[5] = 0x06;
	app_data[6] = 0x06;
	app_data[7] = 0x06;
	app_data[8] = 0x06;
	Uart_Tx_buf(app_data, 3);

#else
	char app_data[10];
	app_data[0] = 1;
	app_data[1] = 1;
	app_data[2] = 0x99;
	app_data[3] = 0x04;
	app_data[4] = 0x05;
	app_data[5] = 0x06;
	app_data[6] = 0x06;
	app_data[7] = 0x06;
	app_data[8] = 0x06;
	Uart_Tx_buf(app_data, 3);

#endif
}


void main(void)
{
	int i;
	Init_System();
	Time1_Init();

	Uart_Init();
	
	Zigbee_App_Init();
	Zigbee_wake_up();		
	LCD_I2C_init();
	ADC_INIT();
	

	WPUA5 = 1;
	TRISA5 = 1;

	now_time_f = 0;
	key_flag = 0;
	last_time_f = 0;
	time_f = 0;
	Seg_Init();

	Seg_Set_Data();

	
	DelayXms(15);
	Hly_Temp_Hum_Obtain();
	DelayXms(15);
	Hly_Zigbee_Restart();
	app_init();

	while(1)
	{
		
		HLY_GetTick();
		if(RA5 == 0)
		{
			DelayXms(10);  // 防抖


			if(RA5 == 0)
			{
				if(key_flag == 0)
				{
					last_time_f = now_time_f;
					time_f = 1;
				}
				if(abs(now_time_f - last_time_f) == 5 && time_f == 1)
				{
					_data_tx_pack();
					time_f = 0;
				}
				key_flag = 1;
			}
			
		}
		else
		{
			DelayXms(10);        
			if(RA5 == 1 && key_flag == 1)   //检测弹起
			{
				//_data_tx_pack();             //弹起后处理任务
				//Hly_Temp_Hum_Obtain();
				time_f = 0;
				key_flag = 0;
			}			
		}
#if 0



	//Hly_Temp_Hum_Obtain();
			DelayXms(200);
			App_Run();
			DelayXms(200);
		//	Zigbee_sleep();
		//	asm("sleep");				//进入休眠模式
			//key_on();

			RB5 = 0;
		RB4 = 0;
		
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		_nop();
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		
		_nop();
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		_nop();
		
		RB5 = ~RB5;
		RB4 = ~RB4;

		i = TMR1L;
		if (i == 0xff)
		{
		
			_data_tx_pack();
			TMR1L = 0;
			TMR1H = 0;
			time_f++;
		}
			
#endif	

		
		
		
		

		
#if 1
		if(RXOK_f==1)
		{
			for(i=0;i<8;i++)
			{
					
				TXREG =RxTable[i];

				while(TRMT==0);
			}

			RXOK_f = 0;
		}
#endif
	}
}
void key_on(void)
{
	if(RA5 == 0)
		{
			DelayXms(10);  // 防抖


			if(RA5 == 0)
			{
				last_time_f = now_time_f;
				if(abs(now_time_f - last_time_f) == 5)
				{
					HLY_GetTick();
				}
				key_flag = 1;
			}
			
		}
		else
		{
			DelayXms(10);        
			if(RA5 == 1 && key_flag == 1)   //检测弹起
			{
				//_data_tx_pack();             //弹起后处理任务
				Hly_Temp_Hum_Obtain();
				time_f = 0;
				
			}			
		}

}


/***********************************************
函数名称：Init_System
函数功能：系统初始化
入口参数：无
出口参数：无
备注：
************************************************/
void Init_System()
{
	asm("nop");
	asm("clrwdt");
	INTCON = 0;					//系统初始化
	OSCCON = 0X70;				//配置振荡为16M,关WDT
	OPTION_REG = 0;

	PEIE = 1;
	RCIE = 1;
	GIE  = 1;

}

void Sleep_Mode()
{
	INTCON = 0;		
	
	OPTION_REG = 0;

	TRISA = 0B00000000; 		//关闭所有输出
	PORTA = 0B00000000;
	WPUA  = 0B00000000;
	
	TRISB = 0B00001000;	
	PORTB = 0B00000000;
	
	PORTB = 0;
	WPUB  = 0B00001000;		 //RB3 上拉
	
	ADCON0 = 0;					//关闭所有模块
	PWMCON0 = 0;
	
	EECON1 = 0;					//清此寄存器,有好处

   			
   	IOCB = 0B00001000;			//允许RB3的IO口电平变化中断
	RBIE = 1;					//允许PORTB电平变化中断
	GIE = 1;					//GIE = 0时，唤醒后执行SLEEP后程序;GIE = 1时，唤醒后跳至中断服务
	
	
	OSCCON = 0X70;				//配置振荡为16M,关闭WDT
	
	PORTB;						//读PORTB值并锁存			
	asm("clrwdt");

	asm("sleep");				//进入休眠模式
	
	asm("nop");
	
}


/***********************************************
函数名称：Time1_Init
函数功能：time1初始化
入口参数：无
出口参数：无
备注：
************************************************/

void Time1_Init()
{
	
	
	
	T1GINV = 0;
	
	TMR1GE = 0;	
	
	T1CKPS0 = 0;
	T1CKPS1 = 0;
	
	
	T1SYNC = 1;

	
	TMR1CS = 1;
	
	T1OSCEN = 1;
	TMR1ON = 1;
	
	DelayXms(100);
	TMR1H = 0;
	TMR1L = 0;

	TMR1IE = 1;


	ANS8 = 1;
	ANS9 = 1;	
	DelayXms(10);

}


/***********************************************
函数名称：PB_Isr
函数功能：PB电平变化中断服务
入口参数：无
出口参数：无
备注：
************************************************/
void interrupt PB_Isr()
{
	if(RBIF)
	{
		RBIF = 0;			//清中断标志
		RB6 = ~RB6;
	
	}
	if (TMR1IF)
	{
		TMR1L = 0;
		TMR1H = 0;
		TMR1IF = 0;
		_data_tx_pack();
	}

#if 0
	if(RCIF==1)					// 接收中断
	{
		uart_cmd_rx_cplt_callback();
	}

#else
	static unsigned char RxNum=0,TEMP;

	if(RCIF==1)					// 接收中断
	{
		RCIF = 0;
	    
		if(RXOK_f==0)	
		{
			RxTable[RxNum] = RCREG;
			RxNum++;
			if(RxNum > 7)
			{
				RxNum =0;
				RXOK_f =1;
			}
		}
		else
		TEMP =  RCREG;
		
	}
#endif
}

/***********************************************
函数名称：HLY_GetTick
函数功能：获取当前时间
入口参数：无
出口参数：无
备注：
************************************************/

void HLY_GetTick(void)
{
	if(TMR1H == 0x7f)
	{
		TMR1L = 0;
		TMR1H = 0;
		now_time_f++;
		if(now_time_f == 200)
		{
			now_time_f = 0;
		}
	}

}

